<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\common\ServiceResponse;
use app\admin\model\PurchaseLogistics as PurchaseLogisticsModel;

class PurchaseLogistics extends BaseService
{


    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new PurchaseLogisticsModel();
    }

    use ServiceResponse;

    /**
     * @describe
     * @param [type] @required
     * @return Renderable
     * @author: Bobo
     * @date: 2024-10-10 14:09:48
     */
    public function getList(array $where, $field = '*', int $page = 1, int $limit = 100, string $from = 'detail')
    {
        if (0 >= $page || 100 < $limit) {
            return $this->error(100002);
        }
        $where['is_del'] = 0;

        $total = $this->model->where($where)->count();
        $list = $this->model
            ->where($where)
            ->field($field)
            ->page($page, $limit)
            ->select()
            ->toArray();
        if (empty($list) && $from === 'logistics') {
            $list = [
                [
                    'purchase_order_id' => $where['purchase_order_id'] ?? 0,
                    'amount' => 0,
                    'name' => '',
                    'order_number' => '',
                    'remark' => ''
                ]
            ];
        }
        $data = ['list' => $list, 'total' => $total];
        return $this->success($data);
    }


    /**
     * @describe
     * @param [type] @required
     * @return Renderable
     * @author: Bobo
     * @date: 2024-10-10 14:33:47
     */
    public function save($data)
    {
        try {
            $res = $this->model->create($data);
            if ($res) {
                $this->logger->logOperation($res->id, 'PurchaseLogistics', '新增', '新增采购物流信息');
                return $this->success();
            } else {
                return $this->error(500001);
            }
        } catch (\Throwable $th) {
            return $this->error(500001, '服务器内部错误：' . $th->getMessage());
        }
    }

    /**
     * @describe
     * @param [arr] @required $where
     * @param [arr] @required $data
     * @return Renderable
     * @author: Bobo
     * @date: 2024-10-10 16:58:32
     */
    public function update($where, $data)
    {
        $res = $this->model->where($where)->find();
        if (!$res) {
            return $this->error(500001);
        }
        foreach ($data as $key => $value) {
            if ($res->$key != $value) {
                $res->$key = $value;
            }
        }
        try {
            $res = $this->model->where($where)->update($data);
            if ($res) {
                $this->logger->logOperation((int)$where['id'], 'PurchaseLogistics', '修改', '修改采购物流信息');
                return $this->success();
            } else {
                return $this->error(500001);
            }
        } catch (\Throwable $th) {
            return $this->error(500001, '服务器内部错误：' . $th->getMessage());
        }
    }
}
